<template>
    <el-row>
        <el-col class="btn">
            <el-dialog :visible.sync="dialogVisible" width="625px" :before-close="handleClose" @close="handleClose()">
                <div slot="title" class="dialog-title box box-align-center">
                    <i class="title-icon"></i>
                    <span class="title-text ml10">{{title}}</span>
                </div>
                <el-form ref="form" label-width="80px" class="reset-form">
                    <el-input class="reset-input w100" placeholder="搜索资源名称" v-model="searchText">
                        <i slot="suffix" class="el-input__icon el-icon-search cp" @click="zykList()"></i>
                    </el-input>
                </el-form>
                <div class="user-list radio">
                    <el-checkbox-group v-model="checkedFiles">
                        <el-checkbox v-for="(item,index) in page.list" :label="item.id" :key="index">
                            <div class="list box box-align-center box-pack-start">
                                <div class="icon box box-align-center box-pack-center mr20">
                                    <el-image src="/static/image/ldkc/icon_video.png" class="image" v-if="item.type == `mp4`"></el-image>
                                    <el-image src="/static/image/ldkc/pdf.png" class="image" v-if="item.type == `pdf`"></el-image>
                                    <el-image src="/static/image/ldkc/ppt.png" class="image" v-if="item.type == `ppt`"></el-image>
                                    <el-image src="/static/image/ldkc/word.png" class="image" v-if="item.type == `word`"></el-image>
                                </div>
                                <div class="box box-tb box-align-center box-pack-start">
                                    <span>{{item.fileName}}</span>
                                    <div>
                                        <span>{{item.userName}}</span>| <span> {{item.createDate}}  上传</span>
                                    </div>
                                </div>
                            </div>
                        </el-checkbox>
                    </el-checkbox-group>
                </div>
                <span slot="footer" class="dialog-footer box box-pack-between btn">
                    <el-upload class="upload-demo" :action="uploadfileurl" :show-file-list="false" :limit="1"
                        :on-progress="progress" ref="upload" :on-success="success" :auto-upload="true"
                        :before-upload="beforeUpload">
                        <el-button class="reset-upload-btn" icon="el-icon-upload">本地上传</el-button>
                    </el-upload>
                    <el-progress v-if="showProgress" :percentage="progInt" style="width: 12.5rem;"></el-progress>
                    <div>
                        <el-button class="reset-cancel-btn" @click="handleClose()">取 消</el-button>
                        <el-button class="reset-comfirm-btn" @click="onSubmit()">确 定</el-button>
                    </div>
                </span>
            </el-dialog>
        </el-col>
    </el-row>
</template>

<script>
    export default {
        props: {
            dialogVisible: {
                default: false
            },
            title: {
                type:String,
				default:'添加'
            },
            jyzid:{
                type:Number
            },
            stageid:{
                type:Number
            }
        },
        data() {
            return {
                fileid:0,
                fileType:"",
                searchText: '',
                page: {
                    total: 0,
                    pageSize: 10,
                    pageNum: 1,
                    list: []
                },
                checkedFiles: [],
                loginInfo: this.$store.state.loginInfo,
                uploadfileurl: this.$url.baseURL + '/upload/uploadfile',
                showProgress: false,
                progInt: 0
            };
        },
        methods: {
            zykList(pageNum){
                var params = {
                    jyzid: this.jyzid,
                    type:"0",
                    searchtext:this.searchText,
                    pageNum:pageNum
                };
                this.$api.jyz.zykList(params).then(res => {
                    if (res.code == '0') {
                        this.page = res.list;
                    } else {
                        this.$message.error('操作失败！');
                    }
                })
            },
            changeFileType(type){
                this.fileType = type;
            },
            handleClose() {
                this.$emit('clickClose')
            },




            onSubmit() {
                var params = {
                    stageid:this.stageid,
                    type:this.fileType,
                    trid:this.loginInfo.user.id,
                    fileid:this.fileid,
                    fileIds:this.checkedFiles
                };
                this.$api.jyz.addKj({str:JSON.stringify(params)}).then(res => {
                    if (res.code == '0') {
                        this.$message.success('操作成功！');
                        this.$emit('listCousSatge');
                        this.handleClose();
                    } else {
                        this.$message.error('操作失败！');
                    }
                })
            },

            progress(event) {
                this.showProgress = true;
                this.progInt = parseInt(event.loaded / event.total * 100, 10);
            },
            success(response, file, fileList) {
                this.fileid = response.body.data.fileid;
            },
            beforeUpload(file) {
                const fileName = file.name;
                const index = fileName.lastIndexOf(".");
                const ext = fileName.substr(index + 1);
                const arr = ["pdf","doc","docx","ppt","pptx"];
                const isOk = arr.includes(ext);
                if (!isOk) {
                    this.$message.warning('课件格式暂仅支持pdf,doc,docx,ppt,pptx 格式！');
                    return false;
                }
                var imgSize = file.size / (1024 * 1024);
                if (imgSize > 200) {
                    this.$message.warning(`当前文件${ parseInt(imgSize) }M,上传文件不能超过200M`)
                    return false
                }
            },
        }
    };
</script>

<style scoped lang="scss">
    .upload-icon {
        width: 110px;
        height: 110px;
        background-color: #F0F1F8;
        line-height: 110px;
        color: #626BF1;
        font-size: 30px;
        border-radius: 10px;
    }

    .avatar {
        width: 110px;
        height: 110px;
    }

    .w100 {
        width: 100%;
    }

    .reset-form {
        padding: 0 20px;
    }


    .user-list {

        .checkbox__label {
            width: 80%;
        }

        .list {
            width: 100%;
            padding: 10px 0;

            .icon {
                width: 56px;
                height: 56px;
                background: #ECEAFF;
                border-radius: 10px;

                .image {
                    width: 27px;
                    height: 31px;
                }
            }
        }

        .item {
            display: block;
            width: 120px;
            margin-right: 30px;
        }
    }


    .user-list>>>.el-checkbox {
        padding: 0 20px;
        width: 585px;
        margin-right: 0;
        display: flex;
        justify-content: center;
    }

    .user-list>>>.el-checkbox__label {
        width: 480px;
        .list {
            display: flex;
            color: #000000;
        }
    }

    .user-list>>>.el-checkbox__input {
        display: flex;
        align-items: center;
    }

    .user-list>>>.el-checkbox__input.is-checked .el-checkbox__inner {
        background-color: #626BF1;
        border-color: #626BF1;
    }

    .user-list>>>.el-checkbox__inner:hover {
        border-color: #626BF1;
    }

    .user-list>>>.el-checkbox__input.is-focus,
        {
        border-color: #626BF1;

        .el-checkbox__inner {
            border-color: #626BF1;
        }
    }
</style>
